//
//  AppDelegate.m
//  UpgradeClassWkWebview
//
//  Created by ZCL_GUOKAI on 2022/4/20.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *vc = [[ViewController alloc]init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    _forwardStreams = [[NSMutableDictionary alloc] init];
    _subscriptions = [[NSMutableDictionary alloc] init];
    _sessions = [[NSMutableDictionary alloc] init];
    _users = [[NSMutableDictionary alloc] init];
    // Override point for customization after application launch.
    return YES;
}

- (IRTCConferenceClient*)conferenceClient{
    IRTCSetMinDebugLogLevel(IRTCLoggingSeverityVerbose);
    if (_conferenceClient==nil){
        NSArray<NSString *> *urlStrings = [[NSArray alloc]initWithObjects:@"turn:101.133.235.226:3478?transport=tcp", nil];
        IRTCIceServer* ices = [[IRTCIceServer alloc]initWithUrls:urlStrings username:@"webrtc" password:@"zco123"];
        
        urlStrings = [[NSArray alloc]initWithObjects:@"sturn:101.133.235.226:3478?transport=tcp", nil];
        IRTCIceServer* icesSturn = [[IRTCIceServer alloc]initWithUrls:urlStrings username:@"webrtc" password:@"zco123"];
        NSArray *ice=[[NSArray alloc]initWithObjects:ices, nil];
        IRTCConferenceClientConfiguration* config=[[IRTCConferenceClientConfiguration alloc]initWithIceServers:ice candidateNetworkPolicy:IRTCCandidateNetworkPolicykAll];
        _conferenceClient=[[IRTCConferenceClient alloc]initWithConfiguration:config];
        _conferenceClient.delegate = self;
    }
    IRTCHostInfo *hostInfo = [[IRTCHostInfo alloc] init];
    hostInfo.ip = @"111.206.145.41";
    hostInfo.location = [[IRTCLocationInfo alloc] init];
    hostInfo.location.lat = 39.90469;
    hostInfo.location.lng = 116.40717;
    hostInfo.address = [[IRTCAddressInfo alloc] init];
    hostInfo.address.nation = @"中国";
    hostInfo.address.province = @"江苏省";
    hostInfo.address.city = @"无锡市";
    hostInfo.address.district = @"新吴区";
    hostInfo.address.code = 320200;
    [_conferenceClient setHostInfo:hostInfo];
    return _conferenceClient;
}

- (void)conferenceClientDidReconnect:(IRTCConferenceClient*)client{
    NSLog(@"AppDelegate on conferenceClientDidReconnect");
}

- (void)conferenceClientDidDisconnect:(IRTCConferenceClient*)client{
    NSLog(@"AppDelegate on conferenceClientDidDisconnect");
}

- (void)conferenceClient:(IRTCConferenceClient*)client
            didAddStream:(IRTCRemoteStream*)stream{
    NSLog(@"AppDelegate on stream added");
    if ([stream isKindOfClass:[IRTCRemoteMixedStream class]]) {
        _mixedStream = (IRTCRemoteMixedStream *)stream;
        _mixedStream.delegate = self;
    }
    else{
        _forwardStreams[stream.streamId] = stream;
        stream.delegate = self;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"OnStreamAdded" object:self userInfo:[NSDictionary dictionaryWithObject:stream forKey:@"stream"]];
}

-(void)streamDidEnd:(IRTCRemoteStream*)stream{
    NSLog(@"AppDelegate on stream ended: %@", stream.streamId);
    if ([stream isKindOfClass:[IRTCRemoteMixedStream class]]) {
        //
    }
    else{
        [_forwardStreams removeObjectForKey:stream.streamId];
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"OnStreamRemoved" object:self userInfo:[NSDictionary dictionaryWithObject:stream forKey:@"stream"]];
}

- (void)conferenceClient:(IRTCConferenceClient*)client
       didReceiveMessage:(NSString*)message
                    from:(NSString*)senderId
                      to:(NSString*)targetType{
    NSLog(@"AppDelegate on msg recieved");
}
- (void)conferenceClient:(IRTCConferenceClient*)client
           didAddSession:(IRTCConferenceSession*)session{
    NSLog(@"AppDelegate on session:%@ added", session.sessionId);
    session.delegate = self;
    _sessions[session.sessionId] = session;
}

- (void)sessionDidError:(IRTCConferenceSession*)session {
    NSLog(@"AppDelegate on session:%@ error", session.sessionId);
    [_sessions removeObjectForKey:session.sessionId];
}

- (void)sessionDidEnd:(IRTCConferenceSession*)session {
    NSLog(@"AppDelegate on session:%@ end", session.sessionId);
    [_sessions removeObjectForKey:session.sessionId];
}

- (void)conferenceClient:(IRTCConferenceClient*)client
       didAddParticipant:(IRTCConferenceParticipant*)user{
    NSLog(@"AppDelegate on participant added");
    user.delegate = self;
    _users[user.userId] = user;
}

- (void)participantDidLeave:(IRTCConferenceParticipant*)user {
    NSLog(@"AppDelegate on participant leave");
    [_users removeObjectForKey:user.userId];
}

- (void)publicationDidEnd:(IRTCConferencePublication*)publication {
    NSLog(@"AppDelegate on publicationDidEnd");

}

- (void)publicationDidMute:(IRTCConferencePublication*)publication
                 trackKind:(IRTCTrackKind)kind {
    NSLog(@"AppDelegate on publicationDidMute");
    
}

- (void)publicationDidUnmute:(IRTCConferencePublication*)publication
                   trackKind:(IRTCTrackKind)kind {
    NSLog(@"AppDelegate on publicationDidUnmute");
}

- (void)publicationDidError:(IRTCConferencePublication*)publication
                  errorInfo:(NSError*)error {
    NSLog(@"AppDelegate on publicationDidError");
    
}

- (void)subscriptionDidEnd:(IRTCConferenceSubscription*)subscription {
    NSLog(@"AppDelegate on subscriptionDidEnd");
}

- (void)subscriptionDidMute:(IRTCConferenceSubscription*)subscription
                  trackKind:(IRTCTrackKind)kind {
    NSLog(@"AppDelegate on subscriptionDidMute");
}

- (void)subscriptionDidUnmute:(IRTCConferenceSubscription*)subscription
                    trackKind:(IRTCTrackKind)kind {
    NSLog(@"AppDelegate on subscriptionDidUnmute");
}

- (void)subscriptionDidError:(IRTCConferenceSubscription*)subscription
                   errorInfo:(NSError*)error {
    NSLog(@"AppDelegate on subscriptionDidError");
    
}
@end
